﻿using Exam.infranstuctrue.Dto.Respones;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Exam.Filters
{
    public class ResultAttribute: BaseAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            var c = (context.Result as ObjectResult)?.Value;
            if (c is ApiResult)
            {
                var result = (ApiResult)c;
                if (result.data == null)
                {
                    context.Result = new OkObjectResult(new
                    {
                        code = result.code,
                        message = result.message,
                    });
                }
                else
                {
                    context.Result = new OkObjectResult(new
                    {
                        code = result.code,
                        message = result.message,
                        data = result.data
                    });
                }
            }
        }
    }
}
